import {
    onBeforeUnmount,
    onMounted,
    ref
} from "vue";

let observer;
export default function useObserverHook(ele, callback) {
    onMounted(() => {
        const node = document.querySelector(ele);
        if (node) {
            observer = new IntersectionObserver(entries => {
                callback && callback(entries);
            });
            observer.observe(node);
            console.log('监听', node)
        }
    })
    onBeforeUnmount(() => {
        const node = document.querySelector(ele);
        if (observer && node) {
            // 解绑元素
            observer.unobserve(node);

            // 停止监听
            observer.disconnect();
            console.log('解绑', node)
            observer = null
            node.remove();
        }
    })
}